/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainFrame.java
 *
 * Created on Apr 21, 2011, 1:03:51 PM
 */
package com.bahrie.gui;

import com.bahrie.entity.Kasir;
import com.bahrie.interfaces.InterKasir;
import com.bahrie.provider.ApplicationContextProvider;
import com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel;
import java.awt.Dimension;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import org.springframework.context.ApplicationContext;

/**
 *
 * @author bahrie
 */
public class MainFrame extends javax.swing.JFrame {

    private Kasir kasir;

    /** Creates new form MainFrame */
    public MainFrame() {


        initComponents();
        //  this.setLocationRelativeTo(null);
        this.setExtendedState(JFrame.MAXIMIZED_BOTH);
        ApplicationContextProvider ctxProvider = ApplicationContextProvider.getInstance();
        ctxProvider.getApplicationContext();
        setMenuLogout();
    }

    private boolean cekLogin(String user, String pass) {
        ApplicationContext appContext = ApplicationContextProvider.getInstance().getApplicationContext();
        kasir = ((InterKasir) appContext.getBean("implKasir", InterKasir.class)).getByUsername(user);
        if (kasir != null) {
            if (kasir.getUsername().equals(user) && kasir.getPassword().equals(pass)) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    void setMenuLogout() {
        menuMaster.setVisible(false);
        menuTransaksi.setVisible(false);
        menuLaporan.setVisible(false);
        menuLogout.setEnabled(false);
        menuLogin.setEnabled(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        dialogLogin = new javax.swing.JDialog();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtUsername = new javax.swing.JTextField();
        txtPassword = new javax.swing.JPasswordField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jDesktopPane1 = new javax.swing.JDesktopPane();
        jMenuBar1 = new javax.swing.JMenuBar();
        MenuFile = new javax.swing.JMenu();
        menuLogin = new javax.swing.JMenuItem();
        menuLogout = new javax.swing.JMenuItem();
        menuExit = new javax.swing.JMenuItem();
        menuMaster = new javax.swing.JMenu();
        masterBuku = new javax.swing.JMenuItem();
        masterDistributor = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();
        menuTransaksi = new javax.swing.JMenu();
        transPasok = new javax.swing.JMenuItem();
        transPenjualan = new javax.swing.JMenuItem();
        menuLaporan = new javax.swing.JMenu();
        lapDistributor = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem4 = new javax.swing.JMenuItem();
        lapPasok = new javax.swing.JMenuItem();
        lapPenjualan = new javax.swing.JMenuItem();
        menuHelp = new javax.swing.JMenu();
        helpAbout = new javax.swing.JMenuItem();

        jLabel1.setText("Username :");

        jLabel2.setText("Password :");

        txtUsername.setText("bahri");

        txtPassword.setText("bahri");
        txtPassword.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtPasswordKeyPressed(evt);
            }
        });

        jButton1.setText("Login");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Cancel");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout dialogLoginLayout = new javax.swing.GroupLayout(dialogLogin.getContentPane());
        dialogLogin.getContentPane().setLayout(dialogLoginLayout);
        dialogLoginLayout.setHorizontalGroup(
            dialogLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(dialogLoginLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(dialogLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(dialogLoginLayout.createSequentialGroup()
                        .addGroup(dialogLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(dialogLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtPassword, javax.swing.GroupLayout.DEFAULT_SIZE, 243, Short.MAX_VALUE)
                            .addComponent(txtUsername, javax.swing.GroupLayout.DEFAULT_SIZE, 243, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, dialogLoginLayout.createSequentialGroup()
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        dialogLoginLayout.setVerticalGroup(
            dialogLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(dialogLoginLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(dialogLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(dialogLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(dialogLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().add(jDesktopPane1, java.awt.BorderLayout.CENTER);

        MenuFile.setText("File");

        menuLogin.setText("Login");
        menuLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuLoginActionPerformed(evt);
            }
        });
        MenuFile.add(menuLogin);

        menuLogout.setText("Logout");
        MenuFile.add(menuLogout);

        menuExit.setText("Exit");
        MenuFile.add(menuExit);

        jMenuBar1.add(MenuFile);

        menuMaster.setText("Master");

        masterBuku.setText("Buku");
        masterBuku.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                masterBukuActionPerformed(evt);
            }
        });
        menuMaster.add(masterBuku);

        masterDistributor.setText("Kasir");
        masterDistributor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                masterDistributorActionPerformed(evt);
            }
        });
        menuMaster.add(masterDistributor);

        jMenuItem3.setText("Distributor");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        menuMaster.add(jMenuItem3);

        jMenuBar1.add(menuMaster);

        menuTransaksi.setText("Transaksi");

        transPasok.setText("Pasok");
        transPasok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                transPasokActionPerformed(evt);
            }
        });
        menuTransaksi.add(transPasok);

        transPenjualan.setText("Penjualan");
        menuTransaksi.add(transPenjualan);

        jMenuBar1.add(menuTransaksi);

        menuLaporan.setText("Laporan");

        lapDistributor.setText("Rekap Buku");
        menuLaporan.add(lapDistributor);

        jMenuItem2.setText("Rekap Kasir");
        menuLaporan.add(jMenuItem2);

        jMenuItem4.setText("Rekap Distributor");
        menuLaporan.add(jMenuItem4);

        lapPasok.setText("Rekap Pasok Buku");
        menuLaporan.add(lapPasok);

        lapPenjualan.setText("Rekap Penjualan");
        menuLaporan.add(lapPenjualan);

        jMenuBar1.add(menuLaporan);

        menuHelp.setText("Help");

        helpAbout.setText("About");
        helpAbout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                helpAboutActionPerformed(evt);
            }
        });
        menuHelp.add(helpAbout);

        jMenuBar1.add(menuHelp);

        setJMenuBar(jMenuBar1);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        if (!txtUsername.getText().isEmpty() && !txtPassword.getText().isEmpty()) {
            if (cekLogin(txtUsername.getText(), txtPassword.getText())) {
                if (kasir.getAkses().equals("kasir")) {
                    menuLogin.setEnabled(false);
                    menuLogout.setEnabled(true);
                    menuTransaksi.setVisible(true);
                    transPasok.setVisible(false);
                } else {
                    menuLogin.setEnabled(false);
                    menuLogout.setEnabled(true);
                    menuMaster.setVisible(true);
                    menuTransaksi.setVisible(true);
                    menuLaporan.setVisible(true);

                }
                dialogLogin.setVisible(false);
            } else {
                txtPassword.setText("");
                JOptionPane.showMessageDialog(this, "Username dan password tidak sesuai.");
            }
        } else {

            JOptionPane.showMessageDialog(this, "Username dan password tidak boleh ada yang kosong.");
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        dialogLogin.setVisible(false);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void menuLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuLoginActionPerformed
        // TODO add your handling code here:
        dialogLogin.setSize(338, 190);
        dialogLogin.setLocationRelativeTo(null);
        dialogLogin.setVisible(true);
    }//GEN-LAST:event_menuLoginActionPerformed

    private void masterBukuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_masterBukuActionPerformed
        // TODO add your handling code here:
        MasterBuku mb=new MasterBuku();
        jDesktopPane1.add(mb);
        Dimension screenSize = this.getSize();
        Dimension frameSize = mb.getSize();
        mb.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
        mb.setVisible(true);
    }//GEN-LAST:event_masterBukuActionPerformed

    private void masterDistributorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_masterDistributorActionPerformed
        // TODO add your handling code here:
        MasterKasir mk=new MasterKasir();
        jDesktopPane1.add(mk);
        Dimension screenSize = this.getSize();
        Dimension frameSize = mk.getSize();
        mk.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
        mk.setVisible(true);
        
    }//GEN-LAST:event_masterDistributorActionPerformed

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
        // TODO add your handling code here:
        MasterDistributor md=new MasterDistributor();
        jDesktopPane1.add(md);
        Dimension screenSize = this.getSize();
        Dimension frameSize = md.getSize();
        md.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
        md.setVisible(true);
    }//GEN-LAST:event_jMenuItem3ActionPerformed

    private void helpAboutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_helpAboutActionPerformed
        // TODO add your handling code here:
        About a=new About();
        jDesktopPane1.add(a);
        Dimension d1=this.getSize();
        Dimension d2=a.getSize();
        a.setLocation((d1.width-d2.width)/2,(d1.height-d2.height)/2);
        a.setVisible(true);
    }//GEN-LAST:event_helpAboutActionPerformed

    private void transPasokActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_transPasokActionPerformed
        // TODO add your handling code here:
        TransaksiPasok tp=new TransaksiPasok();
        jDesktopPane1.add(tp);
        Dimension d1=this.getSize();
        Dimension d2=tp.getSize();
        tp.setLocation((d1.width-d2.width)/2, (d1.height-d2.height)/2);
        tp.setVisible(true);
    }//GEN-LAST:event_transPasokActionPerformed

    private void txtPasswordKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtPasswordKeyPressed
        // TODO add your handling code here:
        if(evt.getKeyCode()==evt.VK_ENTER){
            jButton1ActionPerformed(null);
        }
    }//GEN-LAST:event_txtPasswordKeyPressed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        try {
            UIManager.setLookAndFeel(new NimbusLookAndFeel());
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new MainFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu MenuFile;
    private javax.swing.JDialog dialogLogin;
    private javax.swing.JMenuItem helpAbout;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JDesktopPane jDesktopPane1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JMenuItem lapDistributor;
    private javax.swing.JMenuItem lapPasok;
    private javax.swing.JMenuItem lapPenjualan;
    private javax.swing.JMenuItem masterBuku;
    private javax.swing.JMenuItem masterDistributor;
    private javax.swing.JMenuItem menuExit;
    private javax.swing.JMenu menuHelp;
    private javax.swing.JMenu menuLaporan;
    private javax.swing.JMenuItem menuLogin;
    private javax.swing.JMenuItem menuLogout;
    private javax.swing.JMenu menuMaster;
    private javax.swing.JMenu menuTransaksi;
    private javax.swing.JMenuItem transPasok;
    private javax.swing.JMenuItem transPenjualan;
    private javax.swing.JPasswordField txtPassword;
    private javax.swing.JTextField txtUsername;
    // End of variables declaration//GEN-END:variables
}
